package com.lovo.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.stock.bean.*;
import com.lovo.stock.mapper.RoleMapper;
import com.lovo.stock.mapper.RtjMapper;
import com.lovo.stock.mapper.UtrMapper;
import com.lovo.stock.service.IJurisdictionService;
import com.lovo.stock.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleBean> implements IRoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    UtrMapper utrMapper;
    @Autowired
    RtjMapper rtjMapper;

    @Autowired
    IJurisdictionService jurisdictionService;

    @Override
    public IPage<RoleBean> queryRoleByPage(PageInfo<RoleBean> pageInfo) {
        IPage<RoleBean> page=roleMapper.selectPage(new Page<>(pageInfo.getCurPage(), pageInfo.getPageSize()),null);
        return page;
    }

    @Override
    public List<RoleBean> queryAllRole() {
        return roleMapper.selectList(null);
    }

    @Override
    public List<RoleBean> queryRoleByUserId(int userId) {
        return roleMapper.queryRoleByUserId(userId);
    }

    @Override
    public RoleBean queryRoleById(int id) {
        //查找角色同时查找出其所拥有的权限
        RoleBean roleBean=roleMapper.selectById(id);
        //找出其权限
        List<Integer> ids=new ArrayList<>();
        ids.add(id);
        List<JurisdictionBean> jurisdictionBeans=jurisdictionService.queryJurisdictionByRoleIds(ids);
        //权限存入角色
        roleBean.setJurisdictionBeans(jurisdictionBeans);
        //返回对象
        return roleBean;
    }

    @Override
    @Transactional
    public void addRole(List<Integer> jurIds,RoleBean roleBean) {
        //角色表添加同时角色权限中间表添加
        //角色表
        roleMapper.insert(roleBean);
        //获取生成的主键
        int roleId= roleBean.getRoleId();

        //封装入rtj对象中
        List<Rtj> rtjs=new ArrayList<>();
        for (Integer i:jurIds
             ) {
            Rtj rtj=new Rtj();
            rtj.setRtjRoleId(roleId);
            rtj.setRtjJurisdictionId(i);
            rtjs.add(rtj);
        }
        rtjMapper.insertRjt(rtjs);
    }



    @Override
    @Transactional
    public void deleteRoleById(int id) {
        //首先删除角色表
        roleMapper.deleteById(id);
        //删除中间表的该角色
        QueryWrapper q=new QueryWrapper<>();
        q.eq("rtj_role_id",id);
        rtjMapper.delete(q);
        //删除用户角色中间表
        QueryWrapper q1=new QueryWrapper<>();
        q1.eq("utr_role_id",id);
        utrMapper.delete(q1);
    }

    @Override
    @Transactional
    public void updateRole(int roleId, List<Integer> jurIds) {
        //先删除之前的角色权限
        QueryWrapper q=new QueryWrapper<>();
        q.eq("rtj_role_id",roleId);
        rtjMapper.delete(q);

        //修改中间表,添加
        List<Rtj> rtjs=new ArrayList<>();
        for (Integer i:jurIds
        ) {
            Rtj rtj=new Rtj();
            rtj.setRtjRoleId(roleId);
            rtj.setRtjJurisdictionId(i);
            rtjs.add(rtj);
        }
        rtjMapper.insertRjt(rtjs);
    }
}
